﻿using AutoMapper;
using FlyingHat.Core.Entities;
using FlyingHat.Model.Models;
using FlyingHat.Model.Models.Global;
using FlyingHat.Repository.IRepository;
using FlyingHat.Service.IService;

namespace FlyingHat.Service.Service
{
    public class BookshelfService : IBookshelfService
    {
        private readonly IBookshelfRepository bookshelfRepository;
        private readonly ILibraryRepository libraryRepository;
        private readonly IMapper mapper;

        public BookshelfService(IBookshelfRepository bookshelfRepository, IMapper mapper, ILibraryRepository libraryRepository)
        {
            this.bookshelfRepository = bookshelfRepository;
            this.mapper = mapper;
            this.libraryRepository = libraryRepository;
        }

        public async Task<List<BookshelfModel>> GetList(long libraryId)
        {
            var list = await bookshelfRepository.GetListAsync(i => i.LibraryId == libraryId).ConfigureAwait(false);
            var res = mapper.Map<List<BookshelfModel>>(list);
            return res;
        }
        public async Task<IReturnModel> GetList(long libraryId, string? shelfCode, int page, int limit)
        {
            var list = await bookshelfRepository.GetAllAsync();
            if (libraryId > 0)
            {
                list = list.Where(i => i.LibraryId == libraryId);
            }
            if (!string.IsNullOrWhiteSpace(shelfCode))
            {
                list = list.Where(i => i.Code == shelfCode);
            }
            var count = list.Count();
            var res = list.OrderBy(i => i.Code).Skip((page - 1) * limit).Take(limit).ToList();
            var resList = mapper.Map<List<BookshelfModel>>(res);
            var libraryIds = res.Select(i => i.LibraryId).Distinct().ToList();
            var library = await libraryRepository.GetListAsync(i => libraryIds.Contains(i.Id));
            foreach (var item in resList)
            {
                var temp = library.FirstOrDefault(i => i.Id == item.LibraryId);
                if (temp != null)
                {
                    item.LibraryName = $"{temp.Code}-{temp.Name}";
                }
            }
            return ReturnModel.Page(count, resList);
        }

        public async Task<IReturnModel> Delete(long id)
        {
            // TODO
            // 判断书架是否存放了图书

            var flag = await bookshelfRepository.DeleteAsync(id).ConfigureAwait(false);
            return ReturnModel.Result(flag, "删除");

        }

        public async Task<IReturnModel> AddOrUpdate(BookshelfModel model)
        {
            var entity = mapper.Map<Bookshelf>(model);
            var flag = false;
            flag = await bookshelfRepository.ExistAsync(i => i.Id != model.Id && i.LibraryId == model.LibraryId && i.Code == model.Code);
            if (flag)
            {
                return ReturnModel.Fail("图书室书架号重复");
            }
            if (model.Id > 0)
            {
                flag = await bookshelfRepository.UpdateAsync(entity, i => new { i.Code });
            }
            else
            {
                var count = await bookshelfRepository.CountAsync(i => i.LibraryId == model.LibraryId);
                if (count >= 26)
                {
                    return ReturnModel.Fail("图书室最多只能创建26个书架");
                }

                flag = (await bookshelfRepository.AddAsync(entity)) > 0;
            }
            return ReturnModel.Result(flag, "保存");
        }

        public async Task<List<SelectModel>> GetSelect(long libraryId)
        {
            var list = await bookshelfRepository.GetListAsync(i => i.LibraryId == libraryId);
            var res = list.Select(i => new SelectModel { Text = i.Code, Value = i.Id.ToString() }).ToList();
            return res;
        }
    }
}
